﻿//using ExecutiveDashboard_SL1.Web.DomainServices;
using System;
using System.Collections.Generic;
using System.Linq;
using Telerik.Windows.Controls;
using Telerik.Windows.Examples;

namespace ExecutiveDashboard_SL1
{
    public class ViewModel : ViewModelBase
    {
        //private IEnumerable<SalesAmountExtract> selectedAmountsPerProductCategories;
        //private IEnumerable<SalesAmountExtract> selectedAmountsPerCountries;

        private bool isAxisXAutoRange;
        private double axisXStep;
        private double axisXMaxValue = 10000;
        private double axisXMinValue = 10;
        private string axisXDefaultLabelFormat = "MM/dd/yyyy";
        private bool showPointMarks;

        public ViewModel()
        {
            this.selectionStart = new DateTime(2007, 2, 1);
            this.selectionEnd = new DateTime(2007, 4, 1);
            IsSeries1 = true;
            IsSeries2 = true;
        }
        // Check box
        private bool? _IsSeries1;
        public bool? IsSeries1
        {
            get { return _IsSeries1; }
            set
            {
                _IsSeries1 = value;
                if (_IsSeries1 == false)
                    this.IsVisibilitySeries2 = "Hidden";
                else
                    this.IsVisibilitySeries2 = "Visible";
                OnPropertyChanged("IsSeries1");
            }
        }

        private bool? _IsSeries2;
        public bool? IsSeries2
        {
            get { return _IsSeries2; }
            set
            {
                _IsSeries2 = value;
                if (_IsSeries2 == false)
                    this.IsVisibilitySeries1 = "Hidden";
                else
                    this.IsVisibilitySeries1 = "Visible";
                OnPropertyChanged("IsSeries2");
            }
        }
        // 
        private string _isVisibilitySeries1=string.Empty;

        public string IsVisibilitySeries1
        {
            get { return _isVisibilitySeries1; }
            set
            {
                _isVisibilitySeries1 = value;
                OnPropertyChanged("IsVisibilitySeries1");
            }
        }
        private string _IsVisibilitySeries2;
        public string IsVisibilitySeries2
        {
            get { return _IsVisibilitySeries2; }
            set
            {
                _IsVisibilitySeries2 = value;
                OnPropertyChanged("IsVisibilitySeries2");
            }
        }
        private DateTime selectionStart;
        private DateTime selectionEnd;
        private IEnumerable<SalesAmountExtractWrapper> actualTrendData;

        private bool isBusyByProducts;

        public bool IsBusyByProducts
        {
            get
            {
                return isBusyByProducts;
            }
            set
            {
                isBusyByProducts = value;
                this.OnPropertyChanged("IsBusyByProducts");
            }
        }

        private bool isBusyByCountry;

        public bool IsBusyByCountry
        {
            get
            {
                return isBusyByCountry;
            }
            set
            {
                isBusyByCountry = value;
                this.OnPropertyChanged("IsBusyByCountry");
            }
        }

        public IEnumerable<SalesAmountExtractWrapper> ActualTrendData
        {
            get
            {
                return this.actualTrendData;
            }
            set
            {
                this.actualTrendData = value;
                this.OnPropertyChanged("ActualTrendData");
                this.OnPropertyChanged("TargetTrendData");
                this.OnPropertyChanged("MaxActualYValue");
                this.OnPropertyChanged("MinActualYValue");
            }
        }

        public IEnumerable<SalesAmountExtractWrapper> TargetTrendData
        {
            get
            {
                if (this.ActualTrendData != null)
                {
                    return this.ActualTrendData.Where(a => a.TargetValue != null && a.TargetValue != 0d);
                }
                return null;
            }
        }

        public decimal SelectedActualTotalSalesAmount
        {
            get
            {
                return new Random().Next(1000,5000);
                //if (this.SelectedAmountsPerProductCategories == null) return decimal.One;
                //return this.SelectedAmountsPerProductCategories.Sum(a => a.ActualValue);
            }
        }

        public decimal SelectedTargetTotalSalesAmount
        {
            get
            {
                return new Random().Next(1000, 2000);
                //if (this.SelectedAmountsPerProductCategories == null) return decimal.One;
                //return this.SelectedAmountsPerProductCategories.Sum(a => a.TargetValue);
            }
        }

        public decimal MaxSelectedTotalSalesAmount
        {
            get
            {
                var max = Math.Max(this.SelectedActualTotalSalesAmount, this.SelectedTargetTotalSalesAmount);
                return max + max * 0.05M;
            }
        }

        public decimal TotalActualVsTargetDecimalDifference
        {
            get
            {
                return this.SelectedActualTotalSalesAmount - this.SelectedTargetTotalSalesAmount;
            }
        }

        public decimal TotalActualVsTargetPercentageDifference
        {
            get
            {
                return ((this.SelectedActualTotalSalesAmount / this.SelectedTargetTotalSalesAmount) * 100m - 100) / 100m;
            }
        }

        //public IEnumerable<SalesAmountExtract> SelectedAmountsPerProductCategories
        //{
        //    get
        //    {
        //        return this.selectedAmountsPerProductCategories;
        //    }
        //    set
        //    {
        //        this.selectedAmountsPerProductCategories = value;
        //        this.OnPropertyChanged("SelectedAmountsPerProductCategories");
        //        this.OnPropertyChanged("MaxSalesPerProductValue");
        //        this.OnPropertyChanged("Step");
        //        this.OnPropertyChanged("SelectedActualTotalSalesAmount");
        //        this.OnPropertyChanged("SelectedTargetTotalSalesAmount");
        //        this.OnPropertyChanged("MaxSelectedTotalSalesAmount");
        //        this.OnPropertyChanged("TotalActualVsTargetDecimalDifference");
        //        this.OnPropertyChanged("TotalActualVsTargetPercentageDifference");
        //    }
        //}

        //public IEnumerable<SalesAmountExtract> SelectedAmountsPerCountry
        //{
        //    get
        //    {
        //        return this.selectedAmountsPerCountries;
        //    }
        //    set
        //    {
        //        this.selectedAmountsPerCountries = value;
        //        this.OnPropertyChanged("SelectedAmountsPerCountry");
        //        this.OnPropertyChanged("MaxSalesPerCountriesValue");
        //    }
        //}

        public double MaxActualYValue
        {
            get
            {
                if (this.ActualTrendData != null)
                {
                    return Math.Max(this.ActualTrendData.Max(v => v.ActualValue), this.TargetTrendData.Max(v => v.TargetValue.Value));
                }
                return 0;
            }
        }

        public double MinActualYValue
        {
            get
            {
                if (this.ActualTrendData != null)
                {
                    return Math.Min(this.ActualTrendData.Min(v => v.ActualValue), this.TargetTrendData.Min(v => v.TargetValue.Value));
                }
                return 0;
            }
        }

        //public decimal MaxSalesPerProductValue
        //{
        //    get
        //    {
        //        if (this.SelectedAmountsPerProductCategories != null)
        //        {
        //            var maximum = Math.Max(this.SelectedAmountsPerProductCategories.Max(v => v.ActualValue), this.SelectedAmountsPerProductCategories.Max(v => v.TargetValue));
        //            return maximum;
        //        }
        //        return 0;
        //    }
        //}

        //public decimal MaxSalesPerCountryValue
        //{
        //    get
        //    {
        //        if (this.SelectedAmountsPerCountry != null)
        //        {
        //            var maximum = Math.Max(this.SelectedAmountsPerCountry.Max(v => v.ActualValue), this.SelectedAmountsPerCountry.Max(v => v.TargetValue));
        //            return maximum;
        //        }
        //        return 0;
        //    }
        //}

        //public int Step
        //{
        //    get
        //    {
        //        return (int)this.MaxSalesPerProductValue / 4;
        //    }
        //}

        public DateTime SelectionStart
        {
            get
            {
                return this.selectionStart;
            }
            set
            {
                this.selectionStart = value;
                this.OnPropertyChanged("SelectionStart");
            }
        }

        public DateTime SelectionEnd
        {
            get
            {
                return this.selectionEnd;
            }
            set
            {
                this.selectionEnd = value;
                this.OnPropertyChanged("SelectionEnd");
            }
        }

        public bool IsAxisXAutoRange
        {
            get
            {
                return this.isAxisXAutoRange;
            }
            set
            {
                this.isAxisXAutoRange = value;
                this.OnPropertyChanged("IsAxisXAutoRange");
            }
        }

        public double AxisXStep
        {
            get
            {
                return this.axisXStep;
            }
            set
            {
                this.axisXStep = value;
                this.OnPropertyChanged("AxisXStep");
            }
        }

        public double AxisXMinValue
        {
            get
            {
                return this.axisXMinValue;
            }
            set
            {
                this.axisXMinValue = value;
                this.OnPropertyChanged("AxisXMinValue");
            }
        }

        public double AxisXMaxValue
        {
            get
            {
                return this.axisXMaxValue;
            }
            set
            {
                this.axisXMaxValue = value;
                this.OnPropertyChanged("AxisXMaxValue");
            }
        }

        public string AxisXDefaultLabelFormat
        {
            get
            {
                return this.axisXDefaultLabelFormat;
            }
            set
            {
                this.axisXDefaultLabelFormat = value;
                this.OnPropertyChanged("AxisXDefaultLabelFormat");
            }
        }

        public bool ShowPointMarks
        {
            get
            {
                return this.showPointMarks;
            }
            set
            {
                this.showPointMarks = value;
                this.OnPropertyChanged("ShowPointMarks");
            }
        }
        private IList<double> _data;

        public IList<double> Data
        {
            get
            {
                if (this._data == null)
                {
                    this._data = GenDataChar();
                }

                return this._data;
            }
        }

        private List<double> GenDataChar()
        {
            List<double> lst = new List<double>();
            for (int i = 0; i < 3; i++)
            {
                lst.Add(new Random().Next(1, 100));
                System.Threading.Thread.Sleep(100);
            }
            return lst;
        }

        // Region 4 data
        private IList<IEnumerable<double>> _dataRegion4;
        private int _itemsCount;
        private int _seriesCount;

        public IList<IEnumerable<double>> CollectionData
        {
            get
            {
                if (this._dataRegion4 == null)
                {
                    this._dataRegion4 = this.FillSampleChartData(this.SeriesCount, this.ItemsCount);
                }

                return this._dataRegion4;
            }
        }

        public IEnumerable<double> DataRegion4
        {
            get
            {
                return this.CollectionData[0];
            }
        }

        public List<ChartItem> AreaChartSampleData
        {
            get
            {
                List<ChartItem> lst = new List<ChartItem>();
                for (int i = 0; i < 20; i++)
                {
                    ChartItem item = new ChartItem();
                    item.Y = new Random().Next(0, 100);
                    System.Threading.Thread.Sleep(100);
                    item.Y2 = new Random().Next(10, 100);
                    System.Threading.Thread.Sleep(100);
                    item.StartDate = DateTime.Now.AddDays(i);
                    lst.Add(item);
                }
                return lst;
            }
        }

        public List<string> ComboBoxSampleData
        {
            get
            {
                List<string> lst = new List<string>();
                for (int i = 0; i < 5; i++)
                {
                    lst.Add("Project " + i.ToString());
                }
                return lst;
            }
        }

        public List<GridItem> GridSampleData
        {
            get
            {
                List<GridItem> lst = new List<GridItem>();
                int sttImg = 0;
                for (int i = 0; i < 30; i++)
                {
                    sttImg++;
                    string IndicatorImage = string.Empty;
                    IndicatorImage = "Icon/indicator" + sttImg.ToString() + ".jpeg";
                    if (sttImg >= 3)
                        sttImg = 0;
                    GridItem item = new GridItem();
                    item.ProjectName = "Project " + i.ToString();
                    item.Indicator = IndicatorImage;// i.ToString();
                    item.Actual = i.ToString();
                    item.Budget = i.ToString();
                    lst.Add(item);
                }
                return lst;
            }
        }
        public int ItemsCount
        {
            get
            {
                return _itemsCount;
            }
            set
            {
                _itemsCount = value;
            }
        }

        public int SeriesCount
        {
            get
            {
                return _seriesCount;
            }
            set
            {
                _seriesCount = value;
            }
        }

        protected virtual IList<IEnumerable<double>> FillSampleChartData(int seriesCount, int numbOfItems)
        {
            List<IEnumerable<double>> itemsSource = new List<IEnumerable<double>>();

            for (int i = 0; i < seriesCount; i++)
            {
                itemsSource.Add(SeriesExtensions.GetUserData(numbOfItems, i));
            }

            return itemsSource;
        }
    }

    public class GridItem
    {
        public string ProjectName { get; set; }
        public string Indicator { get; set; }
        public string Budget { get; set; }
        public string Actual { get; set; }
    }
    public class ChartItem
    {
        public double Y2 { get; set; }
        public double Y { get; set; }
        public DateTime StartDate { get; set; }
    }
}
